#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Author:luoping
qq:964973791
'''
import os
def ssl():
    amd = [
        ('cfssl_linux-amd64','cfssl'),
        ('cfssljson_linux-amd64','cfssljson'),
        ('cfssl-certinfo_linux-amd64','cfssl-certinfo')
    ]
    for i in amd:
        path = os.system('wget https://pkg.cfssl.org/R1.2/{} -O /usr/bin/{}'.format(i[0],i[1]))
        if path != 0:
            print("\033[1;32mpull下载失败,退出脚本\033[1;m")
            exit(1)
        else:
            print("\033[1;32m下载{}证书成功\033[1;m".format(i[1]))
    os.system('chmod +x /usr/bin/cfssl /usr/bin/cfssljson /usr/bin/cfssl-certinfo')
    files = os.system('git clone https://github.com/kairen/k8s-manual-files.git /root/k8s-manual-files')
    if files != 0:
        print("\033[1;32mgit clone 失败\033[1;m")
        exit(2)
    else:
        print("\033[1;32m克隆成功，准备生成公钥。\033[1;m")
    etcd = os.system('mkdir /etc/etcd/ssl -p && cd /root/k8s-manual-files && cfssl gencert -initca etcd-ca-csr.json | cfssljson -bare /etc/etcd/ssl/etcd-ca')
    if etcd != 0:
        print("\033[1;32mca证书生成失败。\033[1;m")
        exit(3)
    else:
        print("\033[1;32m生成证书成功，在目录 /etc/etcd/ssl\033[1;m")
        os.system('rm -rf rm -rf /etc/etcd/ssl/*.csr')
if __name__ == '__main__':
    ssl()